package cn.lds.health.interceptor;

import cn.lds.health.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
* 设定客户角色拦截器，userId不为1无法访问客户权限内容
*/
public class UserIntercepor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("user");
        //如果登录用户为空，转发到登录页面并提示信息
        if (Objects.nonNull(user) && user.getRoleId() != 1){

            request.setAttribute("msg","该页面只有普通客户可以访问");

            request.getRequestDispatcher("/go_403").forward(request,response);
        }

        return true;
    }
}